import {
	ADD_GOODS,
  INIT_SHOP_CART,
  REDUCE_CART,
  SELECTED_SINGER_GOODS,
  SELECTED_ALL_GOODS, 
  CLEAR_CART,
  USER_INFO,
  INIT_USER_INFO,
  RESET_USER_INFO,
  SELECTED_NO_GOODS,
  DELETE_CART

} from './mutations-type'


import {getStore,setStore,removeStore,} from './../config/global' 
import Vue from 'vue'

export default {
    	//1.往购物车中添加数据
    	[ADD_GOODS](state,{goodsId,goodsName,goodsPrice,pzleixing}){   //加[]调用方法名，所以[ADD_GOODS]是一个方法名
    		 let shopCart = state.shopCart;
             //1.1 判断商品是否存在
             if (shopCart[goodsId]) {   //存在
                    shopCart[goodsId]['num']++;  //存在的话，goodsId的shopCart商品num加1
             }else{    //不存在,给goodsId的shopCart商品创建一个对象，对象属性包括num,id,name,image,price
                  shopCart[goodsId] ={
                       "num":1,
                       "id":goodsId,
                       "name":goodsName,
                       "price":goodsPrice,
                       "checked":1,
                       "pzleixing":pzleixing,
                  }
             }    //  ---else
             //1.2  产生新对象
             state.shopCart = {...shopCart};   
             //1.3 存入本地·
             setStore('shopCart',state.shopCart);
    	},    // ---[ADD_GOODS]


      //2.页面初始化，获取购物车的数据(本地)
      [INIT_SHOP_CART](state){
         //从本地localStorage 拿到键为shopCart的数据
            let initCart = getStore('shopCart');
            if (initCart) {
              state.shopCart = JSON.parse(initCart); //把从本地得到的initCart(此时是字符串类型)转化为JSON格式(对象)放在内存state.shopCart,供页面使用
            }
      },             // ----[INIT_SHOP_CART]
     

      //3.把商品移出购物车
      [REDUCE_CART](state,{goodsId}){
             let shopCart = state.shopCart;  //拿到购物车的所有商品
             let goods = shopCart[goodsId];   //拿到单个商品
             if (goods) {   //找到该商品     
                    if (goods['num']>0) {
                        goods['num']--;
                        //3.1 判断是否只有0个
                         if (goods['num'] === 0) {
                           delete shopCart[goodsId];
                         }    
                   }else{
                    goods = null;
                   }
                        //3.2 同步数据
                        state.shopCart = {...shopCart};
                        setStore('shopCart',state.shopCart);
             }

      },                //   ----[REDUCE_CART]

     //提交订单后，把商品移除购物车
     [DELETE_CART](state,{goodsId}){
          let shopCart = state.shopCart;
         //  console.log(shopCart[goodsId]);
             // let  goods = shopCart[goodsId];           
          //    console.log(shopCart[goodsId]);
         delete shopCart[goodsId];
        //   同步数据
         state.shopCart = {...shopCart};
         setStore('shopCart',state.shopCart);
     },

      //4. 单个商品选中与取消选中
      [SELECTED_SINGER_GOODS](state,{goodsId}){
           let shopCart = state.shopCart;  //拿到购物车的所有商品
           let goods = shopCart[goodsId];   //拿到单个商品
           if (goods) {
                if (goods.checked) {  //存在该属性
                        // goods.checked = !goods.checked;
                        if (goods.checked=1) {
                          goods.checked = 0;
                        }else{
                          goods.checked =1;
                        }
                }else{
                    Vue.set(goods,'checked',1);    //往goods这个对象里面添加checked,默认属性值为true
                }
                //4.1 同步数据
                  state.shopCart = {...shopCart};
                  setStore('shopCart',state.shopCart);
           }
      },           //   ----[SELECTED_SINGER_GOODS]
     

      //5. 所有商品选中
      [SELECTED_ALL_GOODS](state){
           let shopCart = state.shopCart;  //拿到购物车的所有商品
           Object.values(shopCart).forEach((goods,index)=>{
           goods.checked=0;
           });  
          state.shopCart = {...shopCart};
          setStore('shopCart',state.shopCart);
      },          //     ----[SELECTED_SINGER_GOODS]

      
            //5. 所有商品取消选中
      [SELECTED_NO_GOODS](state){
           let shopCart = state.shopCart;  //拿到购物车的所有商品
           Object.values(shopCart).forEach((goods,index)=>{
                   // console.log(goods.checked);
                    goods.checked=1;
                 });  
              state.shopCart = {...shopCart};
             setStore('shopCart',state.shopCart);                 
      },          //     ----[SELECTED_SINGER_GOODS]

      //6. 清空购物车
      [CLEAR_CART](state){
            state.shopCart = null;
            state.shopCart = {...state.shopCart};
            setStore('shopCart',state.shopCart);
      },     //   ----[CLEAR_CART]
     

     //7. 保存用户信息到本地
     [USER_INFO](state,{userInfo}){
        state.userInfo = userInfo;
        setStore('userInfo',state.userInfo);  
     },


     //8.获取用户信息
     [INIT_USER_INFO](state){
        //8.1 获取用户信息
        let userInfo = getStore('userInfo');   //得到的userInfo是一个字符串 
        //8.2 判断
        if (userInfo) {
            state.userInfo = JSON.parse(userInfo);
        }
     },

     //9.退出登录
     [RESET_USER_INFO](state){
        state.userInfo = {};
        removeStore('userInfo');
     }

}        // ---export default